package org.witsoft.localmqexample.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.witsoft.localmq.MessageManager;
import org.witsoft.localmq.MqInstanceFactory;
import org.witsoft.localmq.OffsetManager;
import org.witsoft.localmq.StatsService;
import org.witsoft.localmq.model.Message;
import org.witsoft.localmq.model.Segment;
import sun.jvmstat.perfdata.monitor.protocol.local.LocalVmManager;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Action;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: LogController
 * @Description:
 * @Author: Colin.Lin
 * @Date: 2020/12/27 3:36 AM
 * @Version 1.0
 **/
@RestController
public class LogController {

    @Autowired
    private MessageManager messageManager;

    @Autowired
    private MqInstanceFactory mqInstanceFactory;

    private ObjectMapper jsonMapper = new ObjectMapper();

    /**
     * 生产数据
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/log/push")
    public Object push(HttpServletRequest request) throws IOException{
        String content = request.getParameter("content");
        if(content == null){
            content = "无内容";
        }
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss").format(new Date());
        Map<String,Object> data = new HashMap<>();
        data.put("time", time);
        data.put("userAgent", request.getHeader("User-Agent"));
        data.put("content", content);
        byte[] bytes = jsonMapper.writeValueAsBytes(data);
        messageManager.produce(bytes);
        return "ok";
    }


    /**
     * 消费数据
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/log/poll")
    public Object poll(HttpServletRequest request) throws IOException{
        List<Message> list = messageManager.consume(10);
        return list.stream().map(item->{
            try {
                return jsonMapper.readValue(item.getData(), HashMap.class);
            } catch (IOException e) {
              throw new RuntimeException(e);
            }
        }).collect(Collectors.toList());
    }

    /**
     * 实时数据统计
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/log/stats")
    public Object stats(HttpServletRequest request) throws IOException{
        Map<String,Object> data = new HashMap<>();
        StatsService statsService = mqInstanceFactory.getStatsService();
        data.put("produceOffset", statsService.getProduceOffset());
        data.put("consumeOffset", statsService.getConsumeOffset());
        data.put("brokerQueueSize", statsService.getBrokerQueueSize());
        data.put("brokerQueueDataSize", statsService.getBrokerQueueDataSize());
        return data;
    }

    /**
     * 清理已过期的物理数据
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/log/purge")
    public Object purge(HttpServletRequest request) throws IOException{
        mqInstanceFactory.getMessageManager().purge();
        return "ok";
    }


    /**
     * 所有片段文件情况
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/log/segments")
    public Object segments(HttpServletRequest request) throws IOException{
        Collection<Segment> segments = mqInstanceFactory.getOffsetManager().getSegments();
        return segments;
    }
}
